package com.duckygo.towerdefense.game;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceHolder;

import com.duckygo.towerdefense.game.scenes.SceneManager;

public class GameSurfaceView extends GLSurfaceView implements GLSurfaceView.Renderer, SurfaceHolder.Callback, SensorEventListener {

	    private static GameThread thread;

	    private SurfaceHolder mSurfaceHolder;

	    
	    public GameSurfaceView(Context context, AttributeSet attr) {
	        super(context, attr);
	        
	        setKeepScreenOn(true);
	        
	       mSurfaceHolder = getHolder();
	       mSurfaceHolder.addCallback(this);
	       //mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_HARDWARE);
	        
	        
	        setRenderer(this);
	        setFocusable(true); 
	    }

	    public GameThread getThread() {
	    	if (thread == null) {
	    		thread = new GameThread(getContext());
	    	}
	    	
	        return thread;
	    }
	    
	    @Override
	    public boolean onKeyDown(int keyCode, KeyEvent event) {
	    	return SceneManager.getInstance().onKeyEvent(event, keyCode);
	    }
	    
	    @Override
	    public boolean onKeyUp(int keyCode, KeyEvent event) {
	    	return SceneManager.getInstance().onKeyEvent(event, keyCode);
	    }

	    
	    
	    
	    @Override
	    public boolean onTouchEvent(MotionEvent event) {
	    	return SceneManager.getInstance().onTouchEvent(event);
	    }

		public void onAccuracyChanged(Sensor sensor, int accuracy ) {
		}

		
		public void onSensorChanged(SensorEvent sensor ) {
			SceneManager.getInstance().onSensorEvent(sensor);
		}
		

	    
		@Override
		public void onDrawFrame(GL10 gl) {
			SceneManager.getInstance().draw(gl);
		}

		@Override
		public void onSurfaceChanged(GL10 gl, int width, int height) {
	        SceneManager.updateGL(gl, width, height);
		}

		@Override
		public void onSurfaceCreated(GL10 gl, EGLConfig config) {
	    	if (!getThread().isAlive()) {
	    		thread.start();
	    	}
	    	
	    	SceneManager.getInstance().register(gl, getContext());
		}


	    public void surfaceDestroyed(SurfaceHolder holder) {
	        boolean retry = true;
	        SceneManager.getInstance().unregister();
	        
	        
	        if (thread == null) {
	        	return;
	        }
	        
	        thread.isRunning = false;
	        while (retry) {
	            try {
	                thread.join();
	                retry = false;
	            } catch (InterruptedException e) {
	    	        Log.e("GameSurfaceView", "Cannot interrupt thread ", e);
	            }
	        }
	        
	        thread = null;
	    }
	    

}
